package com.guoyw.springboot.shardingsphere.service;

import com.guoyw.springboot.shardingsphere.entity.Order;
import com.guoyw.springboot.shardingsphere.entity.OrderItem;
import com.guoyw.springboot.shardingsphere.mapper.OrderItemMapper;
import com.guoyw.springboot.shardingsphere.mapper.OrderMapper;
import com.guoyw.springboot.shardingsphere.vo.OrderInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * author: guoyw
 * create: 2020-09-10 11:16
 **/
@Slf4j
@Service
public class OrderServiceImpl{
  
  @Autowired
  OrderMapper orderMapper;
  @Autowired
  OrderItemMapper orderItemMapper;
  
  // 创建订单
  @Transactional
  public long confirmOrder(int sequenceId){
    //创建订单
    Order order = new Order();
    order.setAddressId(sequenceId);
    order.setUserId(sequenceId);
    order.setStatus("创建订单");
    
    orderMapper.insert(order);
    
    int a = 1/0;
    
    //订单对应产品
    OrderItem item = new OrderItem();
    item.setOrderId(order.getOrderId());
    item.setUserId(sequenceId);
    item.setOrderItemId(sequenceId);
    item.setStatus("创建订单");
    orderItemMapper.insert(item);
    
    return order.getOrderId();
  }
  
  public OrderInfoVo selectAll(){
    return new  OrderInfoVo(orderMapper.selectList(null),orderItemMapper.selectList(null));
  }
}
